/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.friend;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.FriendMapper;
import main_com.pojo.LoginBean;
import main_com.pojo.LookFriendBean;
import main_com.pojo.UpdateFriendBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/UpdateFriendMessageController")
public class UpdateFriendMessageController {
//     public void wrong1(){
//        String msg="不允许有空，修改失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void right(){
//        String msg="填写信息合格，修改成功！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }

    @Autowired
    FriendMapper friendMapper;

    @RequestMapping("/updateFriendMessage")
    protected void updateFriendMessage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email = request.getParameter("email");
        String workPlace = request.getParameter("workPlace");
        String place = request.getParameter("place");
        String QQ = request.getParameter("QQ");
        if (email.isEmpty() || workPlace.isEmpty() || place.isEmpty() || QQ.isEmpty()) {
//            wrong1();
            response.sendRedirect("http://localhost/friendManager/updateFriendMessage.jsp");
        } else {
            try {
                String userName = "";
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.size() == 0) {
                    response.sendRedirect("http://localhost/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {
                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();
                    }
                }
                String name = null;
                String number = null;
                ArrayList friendslist3 = (ArrayList) session.getAttribute("friendslist3");
                if (friendslist3 == null || friendslist3.size() == 0) {
                    response.sendRedirect("http://localhost/main/bottom.jsp");
                } else {
                    for (int i = friendslist3.size() - 1; i >= 0; i--) {
                        UpdateFriendBean ff = (UpdateFriendBean) friendslist3.get(i);
                        name = ff.getName();
                        number = ff.getNumber();
                    }
                }
                LookFriendBean lookFriendBean = new LookFriendBean(userName, name, number, email, workPlace, place, QQ);

                friendMapper.updateFriendByPhoneAndName(lookFriendBean);
                // 获取当前页数，默认是第1页
                String pageParam = request.getParameter("page");
                int currentPage = (pageParam != null) ? Integer.parseInt(pageParam) : 1;
                int pageSize = 3; // 每页显示3条数据
                int offset = (currentPage - 1) * pageSize;
                session.setAttribute("currentPage", currentPage);
                ArrayList<LookFriendBean> friendslist = friendMapper.getFriendByUserNameWithPagination(userName, offset, pageSize);
                session.setAttribute("friendslist", friendslist);
                response.sendRedirect("http://localhost/friendManager/lookFriend.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}